package com.sunday.volunteer.admin.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.TimeCount;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.member.base.BaseActivity;
import com.sunday.volunteer.admin.R;
import com.sunday.volunteer.admin.event.LoginEvent;
import com.sunday.volunteer.admin.http.VolunteerClient;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/*
* 忘记密码
* */
public class ForgetPwdActivity extends BaseActivity {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.tv_getCode)
    TextView tvGetCode;
    @Bind(R.id.et_proPhone)
    ClearEditText etProPhone;
    @Bind(R.id.et_code)
    ClearEditText etCode;
    @Bind(R.id.btn_submit)
    Button btnSubmit;

    private TimeCount timeCount;
    private String proPhone,code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd);
        ButterKnife.bind(this);
        titleView.setText("忘记密码");
        timeCount = new TimeCount(60000, 1000, tvGetCode);
    }

    @OnClick({R.id.tv_getCode,R.id.btn_submit})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_getCode:
                proPhone = etProPhone.getText().toString().trim();
                if (StringUtils.isMobileNO(proPhone)) {
                    getCode(proPhone);
                } else {
                    ToastUtils.showToast(mContext, "请输入正确的手机号");
                }
                break;
            case R.id.btn_submit:
                proPhone = etProPhone.getText().toString().trim();
                code = etCode.getText().toString().trim();
                if (TextUtils.isEmpty(proPhone)){
                    ToastUtils.showToast(mContext,"请输入手机号");
                    return;
                }else if (TextUtils.isEmpty(code)){
                    ToastUtils.showToast(mContext,"请输入验证码");
                    return;
                }
                //是否是修改密码,true:修改密码,false:忘记密码
                if (getIntent().getBooleanExtra("resetPwd",false)){
                    intent = new Intent(mContext,RestMemberPwdActivity.class);
                }else {
                    intent = new Intent(mContext,RestPwdActivity.class);
                }
                intent.putExtra("phone",proPhone);
                intent.putExtra("code",code);
                startActivity(intent);
                break;
        }
    }

    //获取验证码
    private void getCode(String phone) {
        showLoadingDialog(0);
        Call<ResultDO> call = VolunteerClient.getVolunteerAdapter().sendCode(phone);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                dissMissDialog();
                if (response.body() == null||isFinish){
                    return;
                }
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext, "验证码已发送");
                    timeCount.start();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, "网络错误");
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (timeCount != null) {
            timeCount.cancel();
        }
    }

    public void onEvent(LoginEvent event){
        finish();
    }
}
